﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Reflection;
using System.ServiceModel.Syndication;
using System.Xml;
using PBDesk.Utilities;

namespace PBDesk.Business.FeedsAggregator.Helper
{
    public class SyndicationHelper
    {
        #region Constants
        public const int DEFAULT_FEED_ITEM_COUNT = 50; 
        private const string DEFAULT_RESOURCE_FILENAME = "FeedsList.xml";
        private const string RESOURCE_FOLDER = "FeedResource";
        #endregion


        #region Resource Helper
        public static XDocument GetXMLResource(string fileName = DEFAULT_RESOURCE_FILENAME)
        {
            XDocument xDoc = null;
            try
            {
                if (fileName.IsNotNullAndNotEmpty())
                {
                    string location = new System.Uri(Assembly.GetExecutingAssembly().CodeBase).AbsolutePath;
                    location = location.Substring(0, location.LastIndexOf("/") + 1) + RESOURCE_FOLDER + "/" + fileName;
                    try
                    {
                        xDoc = XDocument.Load(location);
                    }
                    catch (Exception ex1)
                    {
                        throw new Exception("Error at SyndicationHelper.GetXMLResource() while parsing/loading XML from " + fileName, ex1);
                    }
                }
                else
                {
                    throw new ArgumentNullException("fileName", "Parameter fileName is null or empty.");
                }
            }
            catch(Exception ex)
            {
                
            }
            return xDoc;
        }
        #endregion

        #region SyndicationPerson Helpers
        public static SyndicationPerson GetSyndicationPerson(string name, string email, string url)
        {
            return new SyndicationPerson(name, email, url);
        }
        public static SyndicationPerson GetSyndicationPerson()
        {
            return GetSyndicationPerson("Pinal Bhatt", "self@PBDesk.com", "http://blog.PBDesk.com");
        }
        #endregion

        public static String GetFeedGenerator()
        {
            return "PBDesk Feed Service(feeds.PBDesk.com)";
        }

        public static SyndicationFeed GetSyndicationFeedFromXDocument(XDocument xDoc, int itemCount = DEFAULT_FEED_ITEM_COUNT)
        {
            SyndicationFeed feed = null;
            feed = new SyndicationFeed(from e in xDoc.Root.Elements("item")
                                       select new SyndicationItem                                     
                                           (
                                           (string)e.Attribute("title"),
                                           (string)e.Attribute("content"),
                                           new Uri((string)e.Attribute("url")))); 
 

            
            return feed;
        }
        public static SyndicationFeed GetSyndicationFeedFromUrl(string url, int itemCount = DEFAULT_FEED_ITEM_COUNT)
        {
            SyndicationFeed feed;
            using (XmlReader reader = XmlReader.Create(url))
            {
                feed = SyndicationFeed.Load(reader);
                feed.Items = feed.Items.OrderByDescending(dt => dt.PublishDate).Take(itemCount);
            }
            return feed;


        }
    }
}
